<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src=".././static/js/public-head.js"></script>
</head>
<body >
<form class="layui-form" action="javascript:;" lay-filter="rpt-form"  >
    <div class="layui-form-item">
        <div class="layui-inline">
            <button class="layui-btn" lay-submit  id="rpt-query"  lay-filter="rpt-query">查询</button>
        </div>
    </div>
    <hr>
</form>
</body>
<script type="text/javascript">
    layui.config({
        base: basePath+'/layui/lay/modules/'
    }).extend({
        formSelects: 'formSelects-v4'
    }).use([ 'table','form','formSelects' ,'element','laydate'  ,'util','code','upload'], function() {
        var $ = layui.$;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var layer = layui.layer;
        var table = layui.table;
        var element = layui.element;
        var laydate= layui.laydate;
        var indexLoad = layer.load();
        $.getJSON(basePath+"/xdWidgetPanel/viewPanelEchartLast"+urlParam,function(rst){
            layer.close(indexLoad)
            if(rst.code !=0){
                layer.alert("异常:<br>"+rst.msg,{icon:2})
                return
            }
            var chartId = "dom-"+ rst.domId
            var actionParam = rst.actionParam
            var option = rst.data
            var allSelect = {} //所有的select
            var eventMap = {}
            $.each(rst.formItem,function(index,item){
                $("#rpt-query").before(item.html)
                if(item.classFlag == "xd-date"){
                   laydate.render({elem:"#"+item.name,type:item.type,value: new Date(commonUtil.setDefaultDate(item.defaultValue)),theme:item.theme})
                }else if(item.classFlag =="xd-select" ){
                    allSelect[item.name] = item
                    if(item.events!=null && item.events !== ""){
                        eventMap[item.name] = item
                    }
                }
            })
            viewPanelTableUtil.initSelect(formSelects ,allSelect,eventMap)
            form.render('checkbox');
            form.render('select');
            form.render('radio');
            $("body").append(rst.panel)
            $("#"+chartId).height(window.innerHeight* actionParam.height * 0.01)
            var myChart = echarts.init(document.getElementById(chartId));
            $(window).resize(function () {
                $("div[class^='xd-panel-echart-']").height(window.innerHeight * actionParam.height * 0.01)
                myChart.resize();
            })
            viewPanelTableUtil.bindEchartAction(myChart,actionParam)
            form.on("submit(rpt-query)", function (obj) {
                viewPanelTableUtil.renderMyChart(myChart,option,obj.field)
            })
            if(actionParam.autoFlag == true){
                setInterval(function(){
                    $("#rpt-query").click()
                },(1000*60) * actionParam.autoInterval );
            }
            if(actionParam.queryFlag  ){
                viewPanelTableUtil.renderMyChart(myChart,option)
            }
        })
    });
var viewPanelTableUtil = {
    renderMyChart:function(myChart,option,formValue){//生成chart-series并画图
        $.post(basePath+"/xdWidgetPanel/queryPanelEchartData"+urlParam,formValue||{}, function (rst) {
            if(rst.code == 0){
                option.series = rst.data
                //var dataArray = []
                $.each(option.series,function(idx,item){
                    if(item.symbolSizeEval!="" && item.symbolSizeEval != null && item.symbolSizeEval != undefined ){
                        item.symbolSize =  function(data){
                            return  eval(item.symbolSizeEval)
                        }
                    }
                })
                if(rst.radar){
                    option.radar = rst.radar
                }
                console.log(option)
                myChart.setOption(option,true)
            }else{
                layer.alert(rst.msg)
            }
        },'json')
    },
    bindEchartAction:function(myChart,actionParams){//echart事件
        if(actionParams.echartAction){
            myChart.on("click",function(params){
                var url = actionParams.echartAction
                var chartParam = [ "seriesName", "name", "value" ]
                var formValue = commonUtil.getFormValueNormal('form[lay-filter="rpt-form"]')
                $.each(actionParams,function(key,value){
                    var pstr="&"+key+"="
                    if($.inArray(value, chartParam) !=-1 ){
                        if(value=="value" && url.indexOf(pstr) < 0){//参数去重
                            url = url + pstr + (params[value][1] == undefined ? params[value] : params[value][1])
                        }else{
                            url = url + pstr + params[value]
                        }
                    }else if(value=="condition" && url.indexOf(pstr) < 0 ){//参数去重
                        url = url + pstr + (formValue[key] == undefined ?'':formValue[key])
                    }
                })
                commonUtil.openPage('',url)
            })
        }
    },
    initSelect:function(formSelects,allSelect,eventMap){
        $.each(allSelect,function(key,item){//先初始化
            if(item.serverFlag){
                formSelects.data(item.name, 'server', {//远程根据 keyword 查询
                    type:"post",
                    data:{id:item.id},
                    searchUrl:basePath+"/xdWidgetSelect/selectView",
                    success: function(id, url, searchVal, result){
                    }
                });
            }else{
                formSelects.render(item.name)
            }
        })
        $.each(eventMap,function(eventSelect,item){//添加级联事件
            formSelects.on(eventSelect, function(id, vals, val, isAdd, isDisabled){
                var valueAarray = {}
                $.each(item.events.split(";"),function(index,targetSelect){
                    var eventValue = formSelects.value(eventSelect , 'valStr' )
                    valueAarray[eventSelect] = eventValue
                    if(allSelect[targetSelect] != null){
                        if(allSelect[targetSelect].serverFlag){
                            valueAarray.id = allSelect[targetSelect].id
                            formSelects.config(targetSelect, {
                                data:valueAarray
                            }).data(targetSelect, 'server', {
                                url:basePath+"/xdWidgetSelect/selectView"
                            });
                        }else{
                            formSelects.search(targetSelect, eventValue);
                        }
                    }
                })
            },true);
        })
    }
}
</script>
